import {Login,adminLogin} from '../../request/api/passpord'
const ADMIN_TOKEN_KEY = 'TDKP_O2O_TOKEN';
const state = {
    token: sessionStorage.getItem(ADMIN_TOKEN_KEY),
};
const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token;
        sessionStorage.setItem(ADMIN_TOKEN_KEY, token);
    },
};
const actions = {
    Login({commit}, userInfo) {
        const {token} = userInfo;
        return new Promise((resolve, reject) => {
            Login({check_token:token}).then(
                datas => {
                    commit("SET_TOKEN", datas.token);
                    resolve();
                }).catch(error => {
                reject(error);
            })
        })
    },
    adminLogin({commit}, userInfo) {
        const {username,password} = userInfo;
        return new Promise((resolve, reject) => {
            adminLogin({username:username,password:password}).then(
                datas => {
                    commit("SET_TOKEN", datas.token);
                    resolve();
                }).catch(error => {
                reject(error);
            })
        })
    },
    //登出用户
    logout({commit}) {
        commit("SET_TOKEN", '');
    },

};

export default {
    namespaced: true,
    state,
    mutations,
    actions
}

